﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SourceMaster.Util
{
    public class EnhancedObservableCollection<T>:System.Collections.ObjectModel.ObservableCollection<T>
    {
        private bool disableNotifications = false;
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if(!disableNotifications)
                base.OnCollectionChanged(e);
        }

        public void DisableNotifications()
        {
            disableNotifications = true;
        }

        public void EnableNotifications(bool sendNotification = false)
        {
            disableNotifications = false;
            if (sendNotification)
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void AddRange(IEnumerable<T> collection)
        {
            var tmpEnableNotifications = !disableNotifications;
            if (tmpEnableNotifications)
                disableNotifications = true;
            foreach (T item in collection)
                Add(item);
            disableNotifications = !tmpEnableNotifications;
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool IsEmpty
        {
            get { return Count == 0; }
        }

        public EnhancedObservableCollection() : base() { }

        public EnhancedObservableCollection(IEnumerable<T> collection) : base(collection) { }
    }
}
